package start.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ConfigDialog extends JDialog{
	
	private ActionListener al;
	
	JTextField serviceName;
	JTextField serverHost;
	
	public ConfigDialog(ActionListener listener ){
		this.al = listener;
		this.setTitle("First configuration");
		this.setContentPane(makeConfigPanel());
		this.setResizable(false);
		this.setSize(new Dimension(600,450));
		this.setModal(true);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
		
	}
	
	private JPanel makeConfigPanel(){
		ImagePanel panel = new ImagePanel("images/notepad.jpg",null);
		panel.setLayout(new BorderLayout());
		JPanel littlePanel = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		littlePanel.add(new JLabel("Server address: "),c);
		
		c.gridx = 1;
		c.gridy = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		
		serverHost = new JTextField(25);
		littlePanel.add(serverHost,c);
		littlePanel.setOpaque(false);
		
		c.gridx = 0;
		c.gridy = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		littlePanel.add(new JLabel("Service name: "),c);
		
		c.gridx = 1;
		c.gridy = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		serviceName = new JTextField(25);
		littlePanel.add(serviceName,c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.fill = GridBagConstraints.HORIZONTAL;

		JButton sub = new JButton("Done");
		sub.setIcon(createIcon("images/arrow.png"));
		sub.setActionCommand("SUBMIT");
		sub.addActionListener(al);
		littlePanel.add(sub,c);
		littlePanel.setOpaque(false);
		
		
		panel.add(littlePanel,BorderLayout.CENTER);
		
		return panel;
	}
	
	protected ImageIcon createIcon(String path){
		/*ImageIcon icon = null;
		URL url = getClass().getClassLoader().getResource(path);
		if(url != null)
			icon = new ImageIcon(url); //TODO this is for the jar
		else
			System.err.println("Could not find requested image");
		return icon;*/
		return new ImageIcon(path);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

}
